package com.test.mooan.labama.activity_fragment_boom.fragment_boom;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.mooan.labama.R;
import com.test.mooan.labama.activity_fragment_boom.ServiceFeedbackActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseActivity_boom;
import com.test.mooan.labama.activity_fragment_boom.base_boom.BaseFragment_boom;
import com.test.mooan.labama.adapter_boom.CustomerServiceListAdapter_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.AppShowInfo_boom;
import com.test.mooan.labama.bean_boom.config_info_boom.CountryInfo_boom;
import com.test.mooan.labama.constants_boom.Constant_boom;
import com.test.mooan.labama.dialog_boom.DeleteDataDialog_boom;
import com.test.mooan.labama.dialog_boom.LogoutDialog_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;
import com.test.mooan.labama.utils_boom.http_boom.HttpManager_boom;


public class PersonalCenterFragment_boom extends BaseFragment_boom {

    private ListView customerServiceLv;
    private TextView mobileTv;
    private View deleteDataBtn;

    private CustomerServiceListAdapter_boom customerServiceListAdapter;

    private int listHeight = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageTag = "ME";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_boom, container, false);
        init(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init(View view){

        View serviceFeedbackLayout = view.findViewById(R.id.fragment_personal_center_layout_service_feedback);
        customerServiceLv = view.findViewById(R.id.fragment_personal_center_lv_customer_service);
        TextView customerServiceDescTv = view.findViewById(R.id.fragment_personal_center_tv_customer_service_desc);
        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.fragment_pc_refreshlayout);

        mobileTv = view.findViewById(R.id.fragment_personal_center_tv_mobile);
        updateMobile();

        String customerHint = AppShowInfo_boom.getInstance().gCustomerServiceHint();
        if (!TextUtils.isEmpty(customerHint)) customerServiceDescTv.setText(customerHint);

        serviceFeedbackLayout.setOnClickListener(v -> {
            try {
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_FEEDBACK_CLICK").toUpperCase());
                requireActivity().startActivity(new Intent(requireContext(),ServiceFeedbackActivity_boom.class));
            }catch (Exception e){
                if (Constant_boom.Debug) e.printStackTrace();
            }
        });

        refreshLayout.setOnRefreshListener(() -> {
            HttpManager_boom.getInstance().getAppShowInfo();
            refreshLayout.setRefreshing(false);
        });

        deleteDataBtn = view.findViewById(R.id.fragment_personal_center_btn_delete_data);

        view.findViewById(R.id.fragment_personal_center_btn_logout).setOnClickListener(v -> {
            try {
                Activity activity = requireActivity();
                if (activity instanceof BaseActivity_boom) new LogoutDialog_boom((BaseActivity_boom) requireActivity()).show();
            }catch (Exception e){
                if (Constant_boom.Debug) e.printStackTrace();
            }
        });

        customerServiceListAdapter = new CustomerServiceListAdapter_boom(requireContext());
        customerServiceLv.setAdapter(customerServiceListAdapter);

        updateCustomerService();

    }

    public void updateCustomerService(){

        customerServiceListAdapter.initCustomerService();
        customerServiceListAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = customerServiceLv.getLayoutParams();
        if (listHeight==0) listHeight = layoutParams.height;
        layoutParams.height = customerServiceListAdapter.getCount()*listHeight;
        customerServiceLv.setLayoutParams(layoutParams);
    }

    @SuppressLint("SetTextI18n")
    public void updateMobile(){
        if (mobileTv==null) return;
        mobileTv.setText(CountryInfo_boom.gInstance().gAreaCode() +" "+ UserInfoHelper_boom.gInstance().gHideMobile());
    }

    public void onDropConfigResponse(boolean showDrop){
        if (showDrop){
            deleteDataBtn.setOnClickListener(v -> {
                UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_DELETE_DATA_CLICK").toUpperCase());
                new DeleteDataDialog_boom(requireContext()).show();
            });
        }else deleteDataBtn.setVisibility(View.GONE);
    }

}
